# Contributor: Will Sinatra <wpsinatra@gmail.com>
# Maintainer: Will Sinatra <wpsinatra@gmail.com>
pkgname=lua-lsqlite3
_pkgname=lsqlite3
pkgver=0.9.6
pkgrel=1
pkgdesc="A binding for Lua to the SQLite3 database library"
url="http://lua.sqlite.org"
license="MIT"
arch="all"
depends="sqlite"
_fossilver=v096
_rockrel=1
source="$_pkgname-$pkgver.zip::http://lua.sqlite.org/index.cgi/zip/lsqlite3_$_fossilver.zip?uuid=v$pkgver"
builddir="$srcdir/${_pkgname}_$_fossilver"

_luaversions="5.1 5.2 5.3 5.4"
for _v in $_luaversions; do
	makedepends="$makedepends lua$_v lua$_v-dev lua$_v-libs luarocks$_v sqlite-dev"
	subpackages="$subpackages lua$_v-${pkgname#lua-}:_subpackage"
	checkdepends="$checkdepends lua$_v-lunit"
done

prepare() {
	default_prepare

	for _v in $_luaversions; do
		cp -r "$builddir" "$builddir-$_v"
	done
}

build() {
	local _v; for _v in $_luaversions; do
		msg "Building for Lua $_v..."
		cd "$builddir-$_v"
		luarocks-$_v \
			CC="$CC" \
			CFLAGS="$CFLAGS -fPIC" \
			LUA_INCDIR="$(pkg-config --variable=includedir "lua$_v")" \
			LUA_LIBDIR="$(pkg-config --variable=libdir "lua$_v")" \
			LUA_VERSION="$_v" \
			make --tree "./build-$_v" \
			--deps-mode=none \
			"$_pkgname-$pkgver-$_rockrel.rockspec"
	done
}

check() {
	local _v; for _v in $_luaversions; do
		msg "Testing on lua$_v"

		cd "$builddir-$_v"
		lua$_v test/test.lua
	done
}

package() {
	mkdir -p "$pkgdir"
}

_subpackage() {
	local _v="${subpkgname:3:3}"
	pkgdesc="$pkgdesc (for Lua $_v)"
	depends="lua$_v"
	install_if="$pkgname=$pkgver-r$pkgrel lua$_v"
	local rockdir="$subpkgdir/usr/lib/luarocks/rocks-$_v/$_pkgname/$pkgver-1"
	local libdir="$subpkgdir/usr/lib/lua/$_v/"

	cd "$builddir-$_v"
	mkdir -p "$libdir"
	cp -a lsqlite3.so "$libdir"/

	mkdir -p "$rockdir"
	echo 'rock_manifest = {}' > "$rockdir"/rock_manifest
}

sha512sums="
c90081879f3f094fa86b7fe5894c8fa937d98188b677da68678b3938b7630b9232d831f2d6c64fe63f676a7f1a1a9ae84ce7d8bc74cec6e74150e126a68c2c92  lsqlite3-0.9.6.zip
"
